document.addEventListener('DOMContentLoaded', () => {
  const intervalInput = document.getElementById('interval');
  const saveButton = document.getElementById('save');

  // 加载当前设置
  chrome.storage.sync.get('interval', (data) => {
    const currentInterval = data.interval / 1000; // 转换为秒
    intervalInput.value = currentInterval;
  });

  // 保存设置
  saveButton.addEventListener('click', () => {
    const newInterval = parseInt(intervalInput.value) * 1000; // 转换为毫秒
    if (newInterval >= 1000 && newInterval <= 600000) {
      chrome.storage.sync.set({ interval: newInterval }, () => {
        alert('设置已保存！');
      });
    } else {
      alert('请输入1到600之间的数字');
    }
  });

  // 停止切换
  document.getElementById('stop').addEventListener('click', () => {
    chrome.runtime.sendMessage({ action: 'stop' }, (response) => {
      if (chrome.runtime.lastError) {
        console.error(chrome.runtime.lastError);
      } else {
        alert('已停止自动切换');
      }
    });
  });
});
